#include <rtthread.h>
#include <board.h>
#include <sht3x.h>
#include "hdc1080.h"
#include <string.h>
#include "drv_lcd.h"
#include "sht20.h"

/* 开启DEBUG功能 */
#define DBG_ENABLE
#define DBG_LEVEL DBG_LOG
#define DBG_SECTION_NAME "userapp"

#define DBG_COLOR
#include <rtdbg.h>



void hdc1080_thread_entry(void *parameter)
{
    //sht20_device_t  sht20_device;
    static sht20_device_t dev = RT_NULL;
    float humidity;
    float temperature;

    //dev = sht20_init("i2c2");
                /* read the sensor data */
    //sht3x_device = sht3x_init("i2c2", 0x80);

    //sht20_softreset(dev);


    
    //sht20_softreset(dev);
    //sht20_config(dev,  0x8100);
    //rt_thread_mdelay(1000);
   /* sht20_config(dev,  0x0100);
    rt_thread_mdelay(500);
    sht20_config(dev,  0x1000);
    rt_thread_mdelay(500);
    sht20_config(dev,  0xff00);
    rt_thread_mdelay(500);*/
    //char num[10];

    rt_kprintf("create hdc1080 thread \r\n");
    //humidity = sht20_read_humidity(dev);
    //temperature = sht20_read_temperature(dev);

      //  rt_kprintf("read sht20 sensor humidity   : %d.%d %%\n", (int)humidity, (int)(humidity * 10) % 10);
      //  rt_kprintf("read sht20 sensor temperature: %d.%d \n", (int)temperature, (int)(temperature * 10) % 10);

    //lcd_disp_str_en(10, 150, "humidity:",WHITE,GREEN);
    //lcd_disp_str_en(10, 170, "temperature:",WHITE,GREEN);
    while (1)
    {

       // humidity = sht20_read_humidity(dev);
       // temperature = sht20_read_temperature(dev);

       // rt_kprintf("read sht20 sensor humidity   : %d.%d %%\n", (int)humidity, (int)(humidity * 10) % 10);
       // rt_kprintf("read sht20 sensor temperature: %d.%d \n", (int)temperature, (int)(temperature * 10) % 10);
        humidity=0;
        temperature=0;

       /* if(RT_EOK == sht3x_read_singleshot(sht3x_device))
        {
            //memset(num,0,10);
            //sprintf(num,"humidity  : %d.%d  ",(int)sht3x_device->humidity,(int)(sht3x_device->humidity * 10) % 10);
            //LOG_I("humidity   : %d.%d  ", (int)sht3x_device->humidity, (int)(sht3x_device->humidity * 10) % 10);
            //lcd_disp_str_en(10, 150, num,WHITE,GREEN);

            //memset(num,0,10);
            //sprintf(num,"temperature   : %d.%d  ",(int)sht3x_device->temperature,(int)(sht3x_device->temperature * 10) % 10);
            //LOG_I("temperature: %d.%d\n", (int)sht3x_device->temperature, (int)(sht3x_device->temperature * 10) % 10);
            //lcd_disp_str_en(10, 170, num,WHITE,GREEN);
            rt_kprintf("read sht3x ok.\r\n");
        }
        else
        {
            rt_kprintf("read sht3x fail.\r\n");
            break;
        }*/
        rt_thread_mdelay(1000);
    }




    //rt_device_t temp_dev = RT_NULL;
    //struct rt_sensor_data temp_data;
   // rt_size_t res = 0;
    /*while (1)
    {
        rt_thread_delay(500);
        //rt_kprintf("dht11 run!\r\n");
    }*/
    
}


hdc1080_device_t hdc1080_init(const char *i2c_bus_name, uint8_t AddrInput)
{
    hdc1080_device_t dev;

    RT_ASSERT(i2c_bus_name);

    dev = rt_calloc(1, sizeof(struct hdc1080_device));
    if (dev == RT_NULL)
    {
        LOG_E("Can't allocate memory for hdc1080 device on '%s' ", i2c_bus_name);
        return RT_NULL;
    }

    dev->i2c = rt_i2c_bus_device_find(i2c_bus_name);
    if (dev->i2c == RT_NULL)
    {
        LOG_E("Can't find hdc1080 device on '%s' ", i2c_bus_name);
        rt_free(dev);
        return RT_NULL;
    }

    dev->lock = rt_mutex_create("mutex_hdc1080", RT_IPC_FLAG_FIFO);
    if (dev->lock == RT_NULL)
    {
        LOG_E("Can't create mutex for hdc1080 device on '%s' ", i2c_bus_name);
        rt_free(dev);
        return RT_NULL;
    }

    dev->AddrInput = AddrInput;
    return dev;
}








